<?php

namespace App\Service;

use App\Models\EwLiveRecord;

class LiveRecordService  extends BaseService
{
	const LIVE_TRUE = 1;		//直播标识：是
	const LIVE_FALSE = 0;		//直播标识：否
	const DEL_TRUE = 1;			//删除标识：是
	const DEL_FALSE = 0;		//删除标识：否
	
	public static function getList($params, $count = FALSE)
	{
		$builder = new \Phalcon\Mvc\Model\Query\Builder();
		$builder->columns(['DISTINCT(r.id) AS id,r.video_id,r.title,r.thumb,r.member_id,u.mobile,u.account,r.avatar,r.nickname,r.address,r.room_id,r.watch_count,r.time_span,r.is_live,r.is_del,r.create_time']);
		$builder->addFrom("App\Models\EwLiveRecord", 'r');
		$builder->leftJoin("App\Models\EwMember", 'r.member_id = m.id' , 'm');
		$builder->leftJoin("App\Models\EwUser", 'r.member_id = u.id' , 'u');

		$builder->andWhere("r.is_del = " . self::DEL_FALSE);

		if (isset($params->is_live) && $params->is_live)
			$builder->andWhere("r.is_live = ". $params->is_live);

		//title
		if (isset($params->title) && $params->title)
			$builder->andwhere("r.title LIKE '%" . $params->title . "%'");

		//nickname
		if (isset($params->nickname) && $params->nickname)
			$builder->andwhere("r.nickname LIKE '%" . $params->nickname . "%'");

		//account
		if (isset($params->account) && $params->account)
			$builder->andwhere("u.account LIKE '%" . $params->account . "%'");

		//mobile
		if (isset($params->mobile) && $params->mobile)
			$builder->andwhere("u.mobile LIKE '%" . $params->mobile . "%'");

		if ($count)
			return $builder->getQuery()->execute()->count();

		$builder->limit($params->page_num,($params->p-1)*$params->page_num);
		$result = $builder->getQuery();

		return $result->execute();
	}

	public static function getInfo($id = 0)
	{
		return EwLiveRecord::findFirst($id);
	}

	public static function count()
	{
		return EwLiveRecord::count();
	}

	/**
	 * [save 保存数据]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public static function save($data)
	{
		$row = new EwLiveRecord;
		if (isset($data->pic) && $data->pic && is_array($data->pic) && count($data->pic) > 0){
			$data->pic = implode(',', $data->pic);
		}else{
			$data->pic = NULL;
		}

		if (isset($data->id) && $data->id){
			$row = EwLiveRecord::findFirst($data->id);
		}
		return $row->save((array)$data);
	}

	/**
	 * [delete 删除数据]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function delete($id)
	{
		if (isset($id) && $id)
			$data = EwLiveRecord::findFirst($id);
		
		if ($data){
			$data->is_del = self::DEL_TRUE;
			return $data->save();
		}

		return NULL;
	}

	/**
	 * [restore 恢复数据]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function restore($id)
	{
		if (isset($id) && $id)
			$data = EwLiveRecord::findFirst($id);
		
		if ($data){
			$data->is_del = self::DEL_FALSE;
			return $data->save();
		}

		return NULL;
	}

	/**
	 * [delete 彻底删除数据]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function forever($id)
	{
		if (isset($id) && $id)
			$row = EwLiveRecord::findFirst($id);

		if ($row)
			return $row->delete();

		return NULL;
	}
}